int len(struct ListNode* head)
{
    int n = 0;
    while (head)
    {
        n++;
        head = head->next;
    }
    return n;
}


struct ListNode* removeNthFromEnd(struct ListNode* head, int n) {
    int i = len(head);
    if (n == i)
    {
        return head->next;
    }
    i -= n;
    struct ListNode* prev = NULL;
    struct ListNode* cur = head;
    while (i >= 1)
    {
        i--;
        prev = cur;
        cur = cur->next;
    }
    prev->next = cur->next;
    return head;
}